//模拟在服务器上运行的客户端,用于和后台服务端通信
//这个文件运行在服务器上,和java后台通信


//STATUS_CODE
// 200  上线
// 201  在线
// 500  发出警报
// 202  成功接受服务器信息/EOF
// 203  成功接受客户端信息/EOF
// 204  心跳检测
// 210  开始摄像头观察
// 211  对焦
// 212  观察结束
// 213  启动观察失败

let item = {
  "status": 0,
  "field": ["mid", "time", "md5"],
  "body": {
    "mid": 123,
    "time": "timestamp",
  },
  "md5": "asdasdsadas"//body段(mid+time+salt)计算结果
}

function requestSocket(status_code, mid, timestamp) {
  console.log('requestSocket');
  let md5Str = md5(mid + timestamp.toString() + 'rB97c1IBapnIBurP5q4PyhsufHdz1jqh99GPQS9ctQYZueiaJp9o87CUjaiJ8dmp').toUpperCase();
  let request = null;
  switch (status_code) {
    case 202:
      console.log('准备响应心跳响应包');
      request = {
        "messageType": "HeartBeat Package",
        "field": ["mid", "time", "message"],
        "status": status_code,
        "body": {
          "mid": mid,
          "time": timestamp,
          "message": "dididi,Client Get"
        },
        "md5": md5Str
      }
      return request;
    case 200:
      console.log('准备发送上线请求');
      request = {
        "messageType": "message",
        "status": status_code,
        "field": ["time", "salt", "mid"],
        "body": {
          "mid": mid,
          "time": timestamp,
        },
        "md5": md5Str//body段(mid+time+salt)计算结果
      }
      return request;
    case 210:
      console.log('准备发送观察请求');
      request = {
        "status": status_code,
        "field": ["mid","time"],
        "body": {
          "mid": mid,
          "time": timestamp,
        },
        "md5": md5Str//body段(mid+time+salt)计算结果
      }
      return request;
  }
}

function contents(info) {
  let infoJSON = JSON.parse(info);
  console.log('数据包:status=' + infoJSON.status);
  console.log('info:' + infoJSON);
  let date = new Date();
  let result = null;
  let request = null;
  switch (infoJSON.status) {
    case 202:
      console.log('成功接受服务器信息/EOF   status:' + infoJSON.status);
      break;
    case 203:
      console.log('成功接受服务器信息/EOF   status:' + infoJSON.status);
      break;
    case 204:
      console.log('心跳检测   status:' + infoJSON.status);
      request = requestSocket(202, 512, Date.parse(date) / 1000);
      sock.write(JSON.stringify(request) + '\n', "utf8");
      break;
    case 210:
      console.log('开始摄像头观察   status:' + infoJSON.status);
      date = new Date();
      request = requestSocket(210, 512, Date.parse(date) / 1000);
      sock.write(JSON.stringify(request) + '\n', "utf8");
      break;
    case 211:
      console.log('对焦   status:' + infoJSON.status);
      break;
    case 212:
      console.log('观察结束   status:' + infoJSON.status);
      break;
    case 213:
      console.log('启动观察失败   status:' + infoJSON.status);
      break;
  }
  return result;
}

var net = require("net");
var md5 = require("md5");
// net.Socket
var sock = net.connect({
  port: 8899,
  host: "192.168.104.157",
}, function () {
  console.log('connected to server!');
});

sock.setEncoding('utf8');

//连接成功调用的事件
sock.on("connect", function () {
  console.log("connect success");
  let date = new Date();
  // item.md5 = md5Str.toUpperCase();
  let request = requestSocket(200, 512, Date.parse(date) / 1000)
  // 在这里我们就可以发送数据了
  console.log('准备发送的数据:' + JSON.stringify(request));
  sock.write(JSON.stringify(request) + '\n', "utf8");
});
// end

// 有错误发生调用的事件
sock.on("error", function (e) {
  console.log("error", e);
});

// socket关闭的事件
sock.on("close", function () {
  console.log("close");
});

// 对方发送了关闭数据包过来的事件
sock.on("end", function () {
  console.log("end event");
});

// 当有数据发生的时候，调用;
sock.on("data", function (data) {
  console.log('收到信息');
  console.log(data);
  contents(data);
});